//
//  NoteTableViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/4.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "NoteTableViewController.h"
#import "NoteTableViewCell.h"
#import "NoteModel.h"
#import "TwoLabelView.h"

@interface NoteTableViewController ()

@end

@implementation NoteTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //下两行结合用，自动计算高度
    self.tableView.estimatedRowHeight = 213;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.backgroundColor = RGBA(249, 249, 249, 1);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;

    
    if (self.noteType == NoteTypeCompany) {       
        self.navigationItem.title = NSLocalizedString(@"company_remarks",nil);
    }else if (self.noteType == NoteTypeContact) {
        self.navigationItem.title =  NSLocalizedString(@"contacts_remarks",nil);
    }else if (self.noteType == NoteTypeMeeting) {
        self.navigationItem.title = NSLocalizedString(@"meetings_remarks",nil);
    }
    [self.tableView registerClass:[NoteTableViewCell class] forCellReuseIdentifier:@"NoteTableViewCell"];

    
    TwoLabelView * headerView = [TwoLabelView new];
    headerView.frame = CGRectMake(0, 0, kMainScreenWidth, 60);
    headerView.nameLab.text = [NSString showSpaceStringWithStr:_name];
    headerView.companyLab.text = [NSString showStringWithStr: _companyName];    
    self.tableView.tableHeaderView = headerView;
    [self fetchNoteList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoteTableViewCell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.noteModel = _dataArray[indexPath.row];

    return cell;
}


#pragma mark - network
//获取备注
- (void)fetchNoteList {
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kCompanyDetailNoteListUrl];
    NSDictionary * dic = @{@"parentId": _parentId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        ws.dataArray = [NSArray yy_modelArrayWithClass:[NoteModel class] json:object[@"list"]];
        [ws.tableView reloadData];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}


@end
